package com.example.administrator.funclient.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.funclient.R;
import com.example.administrator.funclient.model.MsgBean;

import java.util.List;

public class MessageAdapter extends Adapter<MessageAdapter.BaseViewHolder> {
    List<MsgBean> mblist;
    String uuId;

    public MessageAdapter(List<MsgBean> mblist, String uuId) {
        this.mblist = mblist;
        this.uuId = uuId;
    }

    private final int TYPE_ME = 1;
    private final int TYPE_NOTME = 2;

    @Override
    public int getItemViewType(int position) {
        if (uuId.equals(mblist.get(position).getUuID()) ) {
            return TYPE_ME;
        } else {
            return TYPE_NOTME;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        BaseViewHolder vh = null;
        if (viewType == TYPE_ME) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_me,parent,false);
            vh=new BaseViewHolder(view);
        } else if (viewType == TYPE_NOTME) {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notme,parent,false);
            vh=new BaseViewHolder(view);
        }
        return vh;
    }


    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.setData(mblist.get(position));
    }

    @Override
    public int getItemCount() {
        return mblist.size();
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        TextView data,name,msg;
        public BaseViewHolder(View itemView) {
            super(itemView);
            data=itemView.findViewById(R.id.data);
            name=itemView.findViewById(R.id.name);
            msg=itemView.findViewById(R.id.msg);
        }
        public void setData(MsgBean mb){
            data.setText(mb.getDate());
            name.setText(mb.getName());
            msg.setText(mb.getClientMsg());
        }
    }


}
